package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.ConsumableDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.OccupationDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.ConsumableInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.OccupationInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchConsumableInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchOccupationInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IOccupationService;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IconsumableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ConsumableServiceImpl implements IconsumableService {
    @Autowired
    private ConsumableDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<ConsumableInfo> selectPageListByQueryGetMap(Page<ConsumableInfo> page, SearchConsumableInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getName())) {
            params.put("name", searchInfo.getName());
        }
        return dao.getQueryPage(page, params);
    }

    public ConsumableInfo selectOneById(String consumableId) {
        String sql = "select * from consumable_info where id= '" + consumableId + "'";
        ConsumableInfo info = sqlMapper.selectOne(sql, ConsumableInfo.class);
        return info;
    }

    public int insert(ConsumableInfo info) {
        String sql = "INSERT INTO `consumable_info` (`name` , `num`,add_time) VALUES" +
                "('" + info.getName().trim() + "' ," + info.getNum() + ",'"+ DateTimeKit.now()+"');";
        return sqlMapper.insert(sql);
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from consumable_info where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    public int update(ConsumableInfo info) {
        return dao.updateById(info);
    }

    public List<Map<String,Object>>  list(){
        String sql = "select id, name from consumable_info order by num asc;";
        return sqlMapper.selectList(sql);
    }
}